﻿/*
 * @author Lee 
 * Time: 20180403 
 * Abstract: SharePoint API的二次封装
 */

using Microsoft.SharePoint.Client;
using System;

namespace SharePointHelper.DLL.CSOM
{
    public class App
    {
        #region 安装APP
        /// <summary>
        /// 安装APP 如果 appInstance.Status == AppInstanceStatus.Initialized, 即安装成功。
        /// </summary>
        /// <param name="context"></param>
        /// <param name="appFullPath"></param>
        /// <returns></returns>
        public static AppInstance DeployApp(ClientContext context, string appFullPath)
        {
            using (var packageStream = System.IO.File.OpenRead(appFullPath))
            {
                var appInstance = context.Web.LoadAndInstallApp(packageStream);
                context.Load(appInstance);
                context.ExecuteQuery();
                return appInstance;
            }
        }
        #endregion

        #region 获取AppInstance(APP相关信息)
        /// <summary>
        /// 获取AppInstance.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public static ClientObjectList<AppInstance> getAPPInstance(ClientContext ctx)
        {
            ClientObjectList<AppInstance> appInstances = AppCatalog.GetAppInstances(ctx, ctx.Web);
            ctx.Load(appInstances);
            ctx.ExecuteQuery();
            foreach (var appInstance in appInstances)
            {
                Console.WriteLine(appInstance.Title);
            }
            return appInstances;
        }
        #endregion
    }
}
